fix: convert format parse errors from Internal Error to user-facing messages#1076
Open
He-Pin wants to merge 1 commit into
Open
fix: convert format parse errors from Internal Error to user-facing messages#1076He-Pin wants to merge 1 commit into
He-Pin wants to merge 1 commit into
Conversation
Motivation: Jsonnet documents std.format as Python-style percent formatting and documents the % operator as shorthand for std.format. Malformed percent-format specs are user input errors: Python raises ValueError, and other Jsonnet implementations report runtime format errors. sjsonnet's scanner/lowering path threw plain JVM exceptions for cases such as "%z", a trailing "%", or an unterminated named label, which surfaced as "Internal Error" with Java stack traces. Modification: Introduce parseFormatOrFail to wrap both cached format-string parsing and legacy lowered-format conversion. Preserve existing sjsonnet Error values, but map NonFatal parser/lowering exceptions to Error.fail at the format expression position. Add regression coverage for std.format invalid conversions, truncated format specs, unterminated named labels, the % shorthand path, and representative successful formats. Result: Malformed format strings now produce user-facing sjsonnet errors without leaking Java stack traces. The % shorthand continues to use the same formatting implementation while keeping its existing operator stack shape. Verified with __.checkFormat, sjsonnet.jvm[3.3.8].test, and sjsonnet.jvm[3.3.8].assembly. References: - https://jsonnet.org/ref/stdlib.html#std-format - https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
adf999b to
ae0293a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation:
Jsonnet documents
std.format(str, vals)as Python-style percent formatting, and documents the%operator as shorthand forstd.format. Malformed percent-format specs are therefore user input errors, not interpreter failures.Before this PR, sjsonnet's format scanner/lowering path could throw plain JVM exceptions for malformed specs such as
%z, a trailing%, or an unterminated named label. Those exceptions surfaced asInternal Errorwith Java stack traces, while Python and other Jsonnet implementations report user-facing format/runtime errors.Modification:
parseFormatOrFailaround both cached format-string parsing and legacy lowered-format conversion.Errorvalues unchanged.NonFatalparser/lowering exceptions toError.fail(..., pos)so malformed format specs are reported at the Jsonnet expression position.%shorthand path, and representative successful formats.Result:
Malformed format strings now produce user-facing sjsonnet errors instead of leaking Java stack traces. The
%shorthand still uses the same formatting implementation; its diagnostic keeps the existing binary-operator stack shape, so it does not add a[std.format]builtin frame. The table below includes the old sjsonnet behavior frommasterbefore this PR.%formattingstd.format("%z", [1])ValueError: unsupported format character zRUNTIME ERROR: Unrecognised conversion type: zRUNTIME ERROR: Unrecognised conversion type: zformat error: unrecognized conversion type: z[std.format] Internal Error, caused byUnrecognized conversion type: z[std.format] Unrecognized conversion type: zstd.format("hello %", [1])ValueError: incomplete formatRUNTIME ERROR: Truncated format code.RUNTIME ERROR: Truncated format code.format error: truncated format code[std.format] Internal Error, caused byTruncated format code at end of string[std.format] Truncated format code at end of stringstd.format("%(key", {key: 1})ValueError: incomplete format keyRUNTIME ERROR: Truncated format code.RUNTIME ERROR: Truncated format code.format error: truncated format code[std.format] Internal Error, caused byUnterminated ( in format spec[std.format] Unterminated ( in format spec"%z" % [1]%zerrorRUNTIME ERROR: Unrecognised conversion type: zRUNTIME ERROR: Unrecognised conversion type: zformat error: unrecognized conversion type: zInternal Error, caused byUnrecognized conversion type: zUnrecognized conversion type: zReferences:
std.formatdocumentation: https://jsonnet.org/ref/stdlib.html#std-format